package com.songtech.auth.resource.service.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.songtech.auth.resource.dao.AuthResMapper;
import com.songtech.auth.resource.dao.CodeGeneratorUtilDao;
import com.songtech.auth.resource.model.Resources;
import com.songtech.auth.resource.service.ResourceService;
import com.songtech.auth.util.CommState;
import com.songtech.base.exception.BusinessException;
import com.songtech.base.universal.dao.BaseDao;

@Service
public class ResourceServiceImpl implements ResourceService {
	
	@Autowired
	private AuthResMapper authResMapper;
	
	@Autowired
	private CodeGeneratorUtilDao codeUtilDao;
	
	@Resource(name = "com.songtech.auth.resource.model.Resources")
	private BaseDao<Resources> resDaoImpl;
	
	@Override
	public int addResource(Resources resource) {
		//如果Pid不为空查询pid对应的code
		String pId = "";
		String pCode = "";
		if(!StringUtils.isEmpty(resource.getParentId())){
			pId = resource.getParentId();
			Resources pResource = queryResById(resource.getParentId());
			if(pResource!=null){
				pCode = pResource.getCode();
			}
		}
		resource.setCode(getResourceCode(pId, pCode));
		resource.setId(resource.getCode());
//		resource.setState(CommState.EFFECTIVE);
		resource.setCreateTime(new Date());
//		authResMapper.insert(resource);
		try {
			resDaoImpl.insert(resource);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BusinessException("新增资源失败");
		}
		return 1;
	}

	@Override
	public Resources queryResById(String resId) {
		return authResMapper.queryResById(resId);
	}

	@Override
	public List<Resources> queryAllResources() {
		return authResMapper.queryAllResources();
	}

	@Override
	public List<Resources> queryResources(Resources res) {
		return authResMapper.queryResources(res);
	}

	@Override
	public List<Resources> queryResourcesByPid(String resId) {
		return authResMapper.queryResourcesByPid(resId);
	}
	
	/**
	 * 停用资源
	 * @param resId
	 * @return
	 */
	@Override
	public int disabledResource(String resId) {
		Resources res = new Resources();
		res.setId(resId);
		res.setState(CommState.INVALID);
		return authResMapper.disabledResource(res);
	}
	
	/**
	 * 启用资源
	 * @param resId
	 * @return
	 */
	@Override
	public int enabledResource(String resId) {
		Resources res = new Resources();
		res.setId(resId);
		res.setState(CommState.EFFECTIVE);
		return update(res);
	}
	
	/**
	 * 删除资源(逻辑删除)
	 * @param resId
	 * @return
	 */
	@Override
	public int deleteResource(String resId) {
		Resources res = new Resources();
		res.setId(resId);
		res.setState(CommState.ISDELETE);
		return authResMapper.disabledResource(res);
	}
	
	/**
	 * 根据ID修改资源信息，只更新有值的字段
	 * @param res
	 * @return
	 */
	@Override
	public int update(Resources res) {
		res.setUpdateTime(new Date());
		return authResMapper.update(res);
	}
	
	/**
	 * 根据ID修改资源信息，更新所有字段，没值的会被置空
	 * @param res
	 * @return
	 */
	@Override
	public int updateResource(Resources res) {
		res.setUpdateTime(new Date());
		return authResMapper.updateResource(res);
	}

	@Override
	public String getResourceCode(String PId, String PCode) {
		try {
			return getCode(CommState.CODEPREFIX_MENU, "resource", "code", "parent_id", PId, PCode);	
		} catch (Exception e) {
			e.printStackTrace();
//			throw new BusinessException("生成功能菜单代码失败");
		}
		return null;
	}
	
	/**
	 * 
	 * 根据表名和列名获取code
	 * @param prex 前缀
	 * @param tableName
	 * @param colName
	 * @param pcolName
	 * @param PId
	 * @return
	 */
	private String getCode(String prex, String tableName, 
			String colName, String pcolName, String PId, String PCode){
		String nextCode = getNextCode(tableName, colName, pcolName, PId);
		if(nextCode == null || "".equals(nextCode)){
			if(PCode != null && !"".equals(PCode)){
				return PCode + "01";
			}else{
				return prex + "01";
			}
		}
		if(nextCode.length() == 1){
			return prex + "01";
		}
		if(nextCode.length()%2 == 1){//少位
			return prex + "0" + nextCode;
		}
		return prex + nextCode;
	}
	private String getNextCode(String tableName, String colName, String pcolName, String PId){
		Map<String, String> param = new HashMap<>();
		param.put("tableName", tableName);
		param.put("colName", colName);
		param.put("pcolName", pcolName);
		param.put("parentId", PId);
		return codeUtilDao.selectNextCodeFromTable(param);
	}

	@Override
	public List<Resources> getResourceById(String resId) {
		return authResMapper.getResourceById(resId);
	}

}
